i want to count the number of alphabet between my input
exmaple : I input AD , i want it show me the answer is 3,since
(A>B>C>D)
i am newbie for C program , pls help
i want to count the number of alphabet between my input
exmaple : I input AD , i want it show me the answer is 3,since
(A>B>C>D)
i am newbie for C program , pls help
Go read the forum guidelines. Pay attention to how to post code, then post your effort.
Quzah.
Hope is the first step on the road to disappointment.
sorry for first post
No problem, Pui.Originally Posted by PUI
The key to the calculation you want to do, is this - letters have a numbered value called ASCII, so A is one number less than B, which is one number less than C, etc. Capital letters are lower in number than any of the small letters, but the small letters have the same numbered arrangement to their values:
so a is one less than b, which is one less than c, etc.
Knowing that relationship exists, we can actually figure out the difference between any two letters, simply by subtracting the smaller of them, from the larger.
So 'D' - 'A' = 3 (not in C code, but mathematically, using their respective numbers).
Here's some tips:
1) you'll want to have a first line of "#include <stdio.h>" so you can do some input, and output in your program.
2) have an "int main(void)" function
3) with opening and closing curly braces enclosing it "{ }"
4) and at the end of your code, a return 0 statement.
And when you post up your program code, use code tags ALWAYS:
[*code]
Cool program here!
[*/code]
But remove the "*" in both tags (those help you see the tags, as words, otherwise, you can't see them).
And welcome to the forum, Pui.
Adak
that mean i need 'D' - 'A' = 3 change to ASCII code in my program ?
No need. Your computer (if it's a personal computer), already probably uses ASCII, so it knows these values. Just something like: gap = 'D' - 'A'; should be enough.Originally Posted by PUI
Naturally, your program has to have a main function, etc., still.
Adak
that means
printf("%c - %c = %d" , first , second , different);
?
seems cant work
That should work fine.Code:printf("%c - %c = %d" , first , second , second - first);
it's ironic considerate rarity patron of love higher knowledge engulfs me...
thx for help
when i type DA it output -3 , how can i change all output to positive number?
Subtract the smaller number from the bigger number. Basic math. If A is 1, then Z is ... bigger. Subtract small from big. Big from small gives a negative.
Quzah.
Hope is the first step on the road to disappointment.
-_-"Originally Posted by quzah
Try this:Originally Posted by PUI
Code:printf("%c - %c = %d" , first , second , abs(second - first));
You may have to include the math.h header file for this to work, depending on your compiler.
it's ironic considerate rarity patron of love higher knowledge engulfs me...